package com.example.zhaohang.fragment;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.zhaohang.adpter.EmailActivtyAdapter;
import com.example.zhaohang.adpter.EmailRecycleAdapter;
import com.example.zhaohang.been.NoticeBeen;
import com.example.zhaohang.contect.UriContect;
import com.example.zhaohang.employmentapp.R;
import com.example.zhaohang.utils.GsonUtil;
import com.example.zhaohang.utils.ToastMake;
import com.example.zhaohang.utils.Utils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.MediaType;

/**
 * Created by 赵航 on 2018/2/27.
 */

public class PagerFragemnt extends Fragment {

    private String             mTitle;
    private RecyclerView       mRecyclerView;
    private TextView           mTextView;
    private SmartRefreshLayout mSmartRefreshLayout;

    private String getData;
    private ProgressDialog mDialog;
    private NoticeBeen mBeen;
    private List<NoticeBeen.ListBean> mList = new ArrayList<>();
    private List<NoticeBeen.ListBean> mActivityList = new ArrayList<>();
    private EmailRecycleAdapter mAdapter;
    private EmailActivtyAdapter mActivityAdapter;

    //这是单例模式,懒汉式
    public static PagerFragemnt getInstance(String title) {
        PagerFragemnt sf = new PagerFragemnt();
        sf.mTitle = title;
        return sf;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.pagerfragemnt, null);

        mSmartRefreshLayout = v.findViewById(R.id.smartrefreshlayout);
        mSmartRefreshLayout.setRefreshHeader(new ClassicsHeader(getContext()));
        //下拉刷新
        mSmartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {

                mSmartRefreshLayout.finishRefresh();
            }
        });

        //上拉加载更多
        mSmartRefreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                mSmartRefreshLayout.finishLoadmore();
            }
        });
        mRecyclerView = v.findViewById(R.id.recycleview);
        mTextView = v.findViewById(R.id.text);





        if (Utils.netConnected(getContext())) {
            haveData(mTitle);
        }
        return v;
    }

    private void haveData(String title) {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        if (TextUtils.equals(title,"1")){
            getData = "1";
            //设置RecyclerVIew的管理器
            mAdapter = new EmailRecycleAdapter(getData,mList);
            mRecyclerView.setAdapter(mAdapter);
            mList.clear();
        }else {
            getData = "2";
            mActivityAdapter = new EmailActivtyAdapter(getContext(),getData, mActivityList);
            mRecyclerView.setAdapter(mActivityAdapter);
            mActivityList.clear();


        }

        Map<String, String> map = new HashMap<>();
        map.put("type", getData);
        final String json = GsonUtil.parseMapToJson(map);
        mDialog = new ProgressDialog(getContext());
        //设置进度条不可取消
        mDialog.setCancelable(false);
        mDialog.setMessage("加载中,请稍等.....");
        mDialog.show();
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpUtils
                        .postString()
                        .url(UriContect.NEWS)
                        .content(json)
                        .mediaType(MediaType.parse("application/json; charset=utf-8"))
                        .build()
                        .execute(new StringCallback() {
                            @Override
                            public void onError(Call call, Exception e, int id) {
                                mDialog.dismiss();
                                ToastMake.showToast(getResources().getString(R.string.name18));
                                isNoData();
                                return;
                            }

                            @Override
                            public void onResponse(String response, int id) {
                                mDialog.dismiss();

                                if (TextUtils.equals(getData,"1")){
                                    mBeen = GsonUtil.parseJsonToBean(response, NoticeBeen.class);
                                    if (mBeen==null){
                                        isNoData();
                                        return;
                                    }

                                    if (!TextUtils.equals(mBeen.getStatus(),"1")){
                                        ToastMake.showToast(mBeen.getMag());
                                        isNoData();
                                        return;
                                    }else {
                                       //获取数据成功
                                        List<NoticeBeen.ListBean> list = mBeen.getList();
                                        if (list.size()==0){
                                            isNoData();
                                        }else {
                                            mList.addAll(list);
                                            mAdapter.notifyDataSetChanged();
                                        }

                                    }
                                }else {
                                    mBeen = GsonUtil.parseJsonToBean(response, NoticeBeen.class);
                                    if (mBeen==null){
                                        isNoData();
                                        return;
                                    }
                                    if (!TextUtils.equals(mBeen.getStatus(),"1")){
                                        ToastMake.showToast(mBeen.getMag());
                                        isNoData();
                                        return;
                                    }else {
                                        //获取数据成功
                                        List<NoticeBeen.ListBean> list = mBeen.getList();
                                        if (list.size()==0){
                                            isNoData();
                                        }else {
                                            mActivityList.addAll(list);
                                            mActivityAdapter.notifyDataSetChanged();
                                        }

                                    }


                                }
                            }
                        });
            }
        }).start();

    }

    /**
     * @tab 判断listview是否有值,进行界面切换
     */

    private void isNoData(){
        mRecyclerView.setVisibility(View.GONE);
        mTextView.setVisibility(View.VISIBLE);
    }

}
